package com.fs.base.jpa.factory;

import java.util.Collection;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.fs.base.jpa.interf.QueryOpt;
import com.fs.base.jpa.interf.QueryOpt.Operator;
import com.fs.base.jpa.vo.LogicalExpression;
import com.fs.base.jpa.vo.QueryExpression;

/**
 * 条件构造器
 * 用于创建条件表达式
 * @Author TiddlerCJ
 */
public class Restrictions {
	/**
	 * 等于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression eq(String fieldName, Object value) {
		if(StringUtils.isEmpty(value))return null;
		if("NULL".equals(value)){ value = ""; }
		return new QueryExpression (fieldName, value, Operator.EQ);
	}
	
	/**
	 * 不等于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression neq(String fieldName, Object value) {
		if(StringUtils.isEmpty(value))return null;
		return new QueryExpression (fieldName, value, Operator.NE);
	}

	/**
	 * 模糊匹配
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression like(String fieldName, String value) {
		if(StringUtils.isEmpty(value))return null;
		return new QueryExpression (fieldName, value, Operator.LIKE);
	}


	/**
	 * 大于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression gt(String fieldName, Object value) {
		if(StringUtils.isEmpty(value))return null;
		return new QueryExpression (fieldName, value, Operator.GT);
	}

	/**
	 * 小于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression lt(String fieldName, Object value) {
		if(StringUtils.isEmpty(value))return null;
		return new QueryExpression (fieldName, value, Operator.LT);
	}

	/**
	 * 大于等于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression gte(String fieldName, Object value) {
		if(StringUtils.isEmpty(value))return null;
		return new QueryExpression (fieldName, value, Operator.GTE);
	}

	/**
	 * 小于等于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	public static QueryExpression lte(String fieldName, Object value) {
		if(StringUtils.isEmpty(value))return null;
		return new QueryExpression (fieldName, value, Operator.LTE);
	}

	/**
	 * 并且
	 * @param criterions
	 * @return
	 */
	/*public static LogicalExpression and(QueryOpt... criterions){
		return new LogicalExpression(criterions, Operator.AND);
	}*/
	/**
	 * 或者
	 * @param criterions
	 * @return
	 */
	public static LogicalExpression or(QueryOpt... criterions){
		return new LogicalExpression(criterions, Operator.OR);
	}
	/**
	 * 包含于
	 * @param fieldName
	 * @param value
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static LogicalExpression in(String fieldName, Collection value) {
		if(CollectionUtils.isEmpty(value)){
			return null;
		}
		QueryExpression[] ses = new QueryExpression[value.size()];
		int i = 0;
		for(Object obj : value){
			ses[i] = new QueryExpression(fieldName,obj,Operator.EQ);
			i++;
		}
		return new LogicalExpression(ses,Operator.OR);
	}
}

